Program rets_and_vol
      character*6 ticker(5000)
      character*8 cusp(5000)
      integer(kind=8):: nshrs(5000),nvalue(5000),jvalue(5000),ivol(5000),numshrs(5000)
      dimension jast(5000),jdate(5000),itickt(5000),age(5000),favg(5000),fmax(5000),fmin(5000),fstd(5000),iperm(5000)
      dimension nep(5000),price(5000),ishrout(5000),vwret(5000),util(5000),retrn(5000),dfee(5000),ntype(1280),turn(5000)
!      open(unit=10,file='e:\FIS\episode_data.txt')
!      open(unit=11,file='e:\FIS\episode_results.txt')
!      open(unit=99,file='e:\FIS\rets_high_low_vol.txt')
      open(unit=10,file='e:\FIS\episode_data_rep.txt')
      open(unit=11,file='e:\FIS\episode_results_rep.txt')
      open(unit=99,file='e:\FIS\rets_high_low_vol_rep.txt')
      iend=0
      do 700 i=1,1264
700        read(11,1100)ntype(i)
1100  format(6x,i1)          
      i=0
1     i=i+1
2     read(10,1000,end=900)nep(i),jast(i),jdate(i),cusp(i),ticker(i),itickt(i),numshrs(i),age(i),favg(i),fmax(i),fmin(i),fstd(i),iperm(i),price(i),ivol(i),retrn(i),ishrout(i),vwret(i),util(i),nshrs(i)      
!      write(6,1000)nep(i),jast(i),jdate(i),cusp(i),ticker(i),itickt(i),numshrs(i),age(i),favg(i),fmax(i),fmin(i),fstd(i),iperm(i),price(i),ivol(i),retrn(i),ishrout(i),vwret(i),util(i),nshrs(i)      
1000 format(1x,i4,2(1x,i8),1x,a8,2x,a6,4x,i9,1x,i14,5(1x,f11.4),1x,i5,1x,f9.2,1x,i10,1x,f10.6,1x,i9,1x,f9.6,1x,f6.2,1x,i13)    
     turn(i)=float(ivol(i))/(1000.*float(ishrout(i)))
      if(nep(i).eq.nep(1)) go to 1
5     icnt=i-1
      if(icnt.lt.10) go to 500
      avgt=0.
      do 10 ii=1,icnt
      avgt=avgt+turn(ii)
10    continue   
      avgt=avgt/float(icnt)
      reth=0.
      retl=0.
      cnth=0.
      cntl=0.
      turnh=0.
      turnl=0.
      do 30 ii=1,icnt
          if(turn(ii).ge.avgt) go to 20
          cntl=cntl+1.
          retl=retl+retrn(ii)
          turnl=turnl+turn(ii)
          go to 30
20        cnth=cnth+1.
          reth=reth+retrn(ii)
          turnh=turnh+turn(ii)
30    continue
      retl=retl/cntl
      reth=reth/cnth
      turnl=turnl/cntl
      turnh=turnh/cnth
      write(99,9900)nep(1),ntype(nep(1)),jast(1),avgt,cntl,turnl,retl,cnth,turnh,reth
      write(6,9900)nep(1),ntype(nep(1)),jast(1),avgt,cntl,turnl,retl,cnth,turnh,reth
9900  format(1x,i4,1x,i1,1x,i8,1x,f10.6,2(1x,f5.0,2(1x,f9.6)))      
      
    
500   nep(1)=nep(i)
      jast(1)=jast(i)
      jdate(1)=jdate(i)
      cusp(1)=cusp(i)
      ticker(1)=ticker(i)
      itickt(1)=itickt(i)
      numshrs(1)=numshrs(i)
      age(1)=age(i)
      favg(1)=favg(i)
      fmax(1)=fmax(i)
      fmin(1)=fmin(i)
      fstd(1)=fstd(i)
      iperm(1)=iperm(i)
      price(1)=price(i)
      ivol(1)=ivol(i)
      retrn(1)=retrn(i)
      ishrout(1)=ishrout(i)
      vwret(1)=vwret(i)
      util(1)=util(i)
      nshrs(1)=nshrs(i)
      turn(1)=turn(i)
      i=1
      go to 1
900   continue
      iend=iend+1
      if(iend.eq.1) go to 5
      stop
end program rets_and_vol